package com.greyshark.lib.bluetooth.parse

import android.bluetooth.BluetoothGattCharacteristic
import com.greyshark.lib.bluetooth.core.CallbackManager
import com.greyshark.lib.bluetooth.extensions.toHexString
import com.greyshark.lib.bluetooth.utils.BleLogger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import java.io.ByteArrayOutputStream

@Deprecated("this is replace by PacketParse")
class PacketDataParse {

  private val tag = PacketDataParse::class.java.simpleName

  // io线程协程作用域
  private val ioScope = CoroutineScope(Dispatchers.IO + Job())

  // 用于同步队列访问
  private val mutex = Mutex()

  companion object {
    val instance: PacketDataParse by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
      PacketDataParse()
    }
  }

  private val infoStream = ByteArrayOutputStream()
  private val dataStream = ByteArrayOutputStream()
  var isCompleteData = false

  fun parsePacket(characteristic: BluetoothGattCharacteristic) {
    val packet = characteristic.value
    BleLogger.i(tag, "parsePacket data=${packet.toHexString()}")
    ioScope.launch {
      mutex.withLock {

        if (packet.size < 2) return@launch

        if (packet[0].toInt() == 0x89
          && packet[1].toInt() == 0x56
          && packet[packet.lastIndex - 1].toInt() == 0xB5
          && packet.last().toInt() == 0x3A
        ) {
          if (packet.size <= 8) {
            BleLogger.i(tag, "parse packet length <= 8")
            return@launch
          }
          for (index in 0 until 4) {
            infoStream.write(packet[index].toInt())
          }
          for (index in packet.size - 4 until packet.size) {
            infoStream.write(packet[index].toInt())
          }
          for (index in 4 until packet.size - 4) {
            dataStream.write(packet[index].toInt())
          }
          isCompleteData = true
        } else {
          if (packet[0].toInt() == 0x89 && packet[1].toInt() == 0x56) {
            for (index in 0 until 4) {
              infoStream.write(packet[index].toInt())
            }
            for (index in 4 until packet.size) {
              dataStream.write(packet[index].toInt())
            }
          } else if (packet[packet.lastIndex - 1].toInt() == 0xB5
            && packet.last().toInt() == 0x3A
          ) {
            for (index in packet.size - 4 until packet.size) {
              infoStream.write(packet[index].toInt())
            }
            for (index in 0 until packet.size - 4) {
              dataStream.write(packet[index].toInt())
            }
            isCompleteData = true
          } else {
            dataStream.write(packet)
          }
        }

        if (isCompleteData) {
          isCompleteData = false
          //校验size
          //校验crc
          val infoPacket = infoStream.toByteArray().copyOf()
          val dataPacket = dataStream.toByteArray().copyOf()
          infoStream.reset()
          dataStream.reset()
          BleLogger.i(tag, "infoPacket =${packet.toHexString()}")
          BleLogger.i(tag, "dataPacket =${packet.toHexString()}")
          val packetType =
            ((infoPacket[2].toInt() and 0xFF) shl 8) or (infoPacket[3].toInt() and 0xFF)
          when (infoPacket[3].toInt()) {
            //查询
//            0x01 -> parseQueryPacketData(infoPacket, dataPacket)
            //设置
//            0x02 -> parseSettingPacketData(infoPacket, dataPacket)
            //主动上报
//            0x03 -> parseActiveReportingPacketData(infoPacket, dataPacket)

            else -> {}
          }
        }
      }
    }
  }

}